﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MainClient
{
    public partial class frmDiancai : Form
    {
        private static string strOperName;

        public frmDiancai(string opeartor,string tableName, string orderID)
        {
            InitializeComponent();
            strOperName = opeartor;
            this.tssl_operator.Text = opeartor;
            this.lbl_tableID.Text = tableName;
            this.tssl_orderID.Text = orderID;
            this.timer1.Start();
        }

        private void Diancai_Load(object sender, EventArgs e)
        {
            // Load menu list
            DataAccess.MenuDAO menuDAO = new DataAccess.MenuDAO();
            System.Data.DataSet ds1 = menuDAO.loadMenu();
            this.dataGridView2.DataSource = ds1.Tables[0].DefaultView;

            // Load order items
            DataAccess.OrderDAO orderDAO = new DataAccess.OrderDAO();
            System.Data.DataSet ds2 = orderDAO.loadOrderItems(this.tssl_orderID.Text.Trim());
            this.dataGridView1.DataSource = ds2.Tables[0].DefaultView;
            // Reflesh mount Value of all items
            this.refleshTotalMoney(orderDAO.getOrderTotalPrice(this.tssl_orderID.Text.Trim()));
        }

        private void dataGridView2_MouseClick(object sender, MouseEventArgs e)
        {
            if (this.dataGridView2.SelectedRows.Count > 0)
            {
                this.txt_foodID.Text = (string) this.dataGridView2.CurrentRow.Cells["编号"].Value;
            }
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            this.toolStripStatusLabel5.Text = DateTime.Now.ToString();
        }

        private void btn_addFood_Click(object sender, EventArgs e)
        {
            string strOrderID = this.tssl_orderID.Text.Trim();
            string strFoodID = this.txt_foodID.Text.Trim();
            if (strFoodID != "" && !strFoodID.Equals(""))
            {
                // Add this item into db.
                DataAccess.OrderDAO orderDAO = new DataAccess.OrderDAO();
                orderDAO.addOrderFood(this.tssl_operator.Text.Trim(),
                                                strOrderID, strFoodID,
                                                Int32.Parse(this.txt_count.Value.ToString()),
                                                this.txt_notes.Text.Trim(), 
                                                this.txt_cookID.Text.Trim());
                System.Data.DataSet ds2 = orderDAO.loadOrderItems(this.tssl_orderID.Text.Trim());
                this.dataGridView1.DataSource = ds2.Tables[0].DefaultView;
                this.refleshTotalMoney(orderDAO.getOrderTotalPrice(strOrderID));
            }

            // 重置初始参数
            this.txt_count.Value = 1;
            this.txt_cookID.Text = "";
            this.txt_notes.Text = "";
        }

        // 刷新总计里的值
        private void refleshTotalMoney(double money)
        {
            if (money > 0)
                this.textBox1.Text = "总计：￥" + string.Format("{0:.00}", money);
            else
                this.textBox1.Text = "总计：￥0.00";
        }

        /**
         * 用户点击提交订单
         */
        private void btn_submitOrder_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount < 1)
            {
                MessageBox.Show("订单内容为空，无法提交！", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }
            // 提示用户是否继续操作？
            if (MessageBox.Show("确定要提交订单吗？",
                            "提示",
                            MessageBoxButtons.OKCancel,
                            MessageBoxIcon.Question) != DialogResult.OK)
                return;

            DataAccess.OrderDAO orderDAO = new DataAccess.OrderDAO();
            int ret = orderDAO.submitOrder(strOperName,
                                this.tssl_orderID.Text.Trim(),
                                this.lbl_tableID.Text.Trim());
            switch (ret)
            {
                case 0:
                    string strShow = "订单:"+this.tssl_orderID.Text.Trim()+"提交成功!";
                    MessageBox.Show(strShow, "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                    break;
                case 1:
                    strShow = "订单:"+this.tssl_orderID.Text.Trim()+" 已经提交, 无法重复提交!";
                    MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                case 2:
                    strShow = "网络连接失败, 提交订单失败！";
                    MessageBox.Show(strShow, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    break;
                default:
                    Console.WriteLine("Exception -> Sumbit Order Error!");
                    break;
            }
        }

        /**
         * 用户双击点餐列表，完成添加该菜品
         */
        private void dataGridView2_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if ( sender.Equals(dataGridView2)) {
                btn_addFood_Click(sender, e);
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            string showstr = "取消点餐，将不保存当前所添加内容?";
            if ( MessageBox.Show(
                        showstr, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation
                    ) == DialogResult.OK )
            {
                this.Close();
                this.Dispose();
            }
        }

        private void btn_CloseAccount_Click(object sender, EventArgs e)
        {
            string strOrderID = this.tssl_orderID.Text.Trim();
            // Judge whether this orderID is submit.
            DataAccess.OrderDAO orderDAO = new DataAccess.OrderDAO();
            if (!orderDAO.isOrderSubmit(strOrderID))
            {
                string strShow = "订单:"+strOrderID+" 未提交, 无法结账!";
                MessageBox.Show(strShow, "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            frmJiezhang jieZhang = new frmJiezhang(
                            this.tssl_operator.Text.Trim(),
                            this.tssl_orderID.Text.Trim(),
                            this.lbl_tableID.Text.Trim());
            jieZhang.Owner = this.Owner;
            this.Close();
            this.Dispose();
            jieZhang.ShowDialog();
        }

        private void btn_EditItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount < 0) return;

            DataGridViewRow selectedRow = this.dataGridView1.SelectedRows[0];
            string strRmFoodID = selectedRow.Cells[0].Value.ToString().Trim();
            frmEditOrderItem dialog1 = new frmEditOrderItem(
                                    this.tssl_orderID.Text.Trim(),
                                    strRmFoodID,
                                    selectedRow.Cells[1].Value.ToString().Trim(),
                                    Int32.Parse(selectedRow.Cells[4].Value.ToString()),
                                    selectedRow.Cells[8].Value.ToString().Trim(),
                                    selectedRow.Cells[9].Value.ToString().Trim());
            if (dialog1.ShowDialog() == DialogResult.OK)
            {
                this.Diancai_Load(sender, e);
            }
        }

        private void btn_RemoveItem_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount < 0) return;

            string strRmFoodID = this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString().Trim();
            if (strRmFoodID != "" || !strRmFoodID.Equals(""))
            {
                string showstr = "您确定要删除 编号: "+strRmFoodID+" ?";
                if (MessageBox.Show(
                            showstr, "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation
                        ) == DialogResult.OK)
                {
                    // Remove food item from orderlist.
                    DataAccess.OrderDAO orderDAO = new DataAccess.OrderDAO();
                    if (orderDAO.removeOrderFood(strOperName, this.tssl_orderID.Text.Trim(), strRmFoodID))
                    {
                        string strShow2 = "删除菜品成功！";
                        MessageBox.Show(strShow2, "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        System.Data.DataSet ds = orderDAO.loadOrderItems(this.tssl_orderID.Text.Trim());
                        this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
                        // Reflesh mount Value of all items
                        this.refleshTotalMoney(orderDAO.getOrderTotalPrice(this.tssl_orderID.Text.Trim()));
                    }
                }
            }
        }

        private void btn_deleteOrder_Click(object sender, EventArgs e)
        {
            if (this.dataGridView1.RowCount == 0)
            {
                try
                {
                    DataAccess.OrderDAO orderDAO = new DataAccess.OrderDAO();
                    orderDAO.removeOrder(LoginUser.getCurrOper(), this.tssl_orderID.Text.Trim());
                    DataAccess.TableDAO tableDAO = new DataAccess.TableDAO();
                    tableDAO.setTableState(this.lbl_tableID.Text.Trim(), DataAccess.TableDAO.STATE_AVAILABLE);
                    MessageBox.Show("删除空订单成功！", "信息",
                                   MessageBoxButtons.OK, MessageBoxIcon.Information);
                    this.Close();
                }
                catch
                {
                    MessageBox.Show("删除空订单失败，请稍候再试！", "错误",
                                   MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            else
            {
                MessageBox.Show("无法删除非空订单！", "错误",
                                  MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        } // end of function

        
    }
}
